package cn.neusoft.service.impl;

import cn.neusoft.bean.Business;
import cn.neusoft.dao.IBusinessDao;
import cn.neusoft.dao.impl.BusinessDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.IBusinessService;
import cn.neusoft.utils.JdbcUtils;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;

import java.sql.Connection;
import java.util.List;

public class BusinessService implements IBusinessService {

    private IBusinessDao businessDao;

    public BusinessService(){
        businessDao = new BusinessDao();
    }

    @Override
    public ResultBean add(Business business) throws Exception {

        //参数校验
        if (business==null ||
            StringUtils.isEmpty(business.getBusinessAccount()) ||
            StringUtils.isEmpty(business.getBusinessName())){
            return ResultBean.ERROR(10001,"params null");
        }

        int i = businessDao.add(business);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("add business fail");
    }

    @Override
    public ResultBean update(Business business) throws Exception {
        //参数校验
        if (business==null ||
            business.getBusinessId()==null ||
            StringUtils.isEmpty(business.getBusinessAccount()) ||
            StringUtils.isEmpty(business.getBusinessName())){
            return ResultBean.ERROR(10001,"params null");
        }

        int i = businessDao.update(business);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("update business fail");
    }

    @Override
    public ResultBean delete(Integer id) throws Exception {
        //参数校验
        if (id==null){
            return ResultBean.ERROR(10001,"params null");
        }

        int i = businessDao.delete(id);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("delete business fail");
    }

    @Override
    public ResultBean getById(Integer id) throws Exception {
        //参数校验
        if (id==null){
            return ResultBean.ERROR(10001,"params null");
        }
        Business business = businessDao.getById(id);
        return business!=null?ResultBean.SUCCESS(business):ResultBean.ERROR("get business by id fail");
    }

    @Override
    public ResultBean getAll() throws Exception {
        List<Business> businessList = businessDao.getAll();
        return businessList!=null?ResultBean.SUCCESS(businessList):ResultBean.ERROR("get all business fail");
    }

    @Override
    public ResultBean getByPage(BaseQuery baseQuery) throws Exception {

        PageBean<Business> pageBean = new PageBean<>();

        //查询总记录
        int count = businessDao.getCount(baseQuery);
        pageBean.setTotalRows(count);

        //计算总页数
        if(count% baseQuery.getPageCount()==0){
            pageBean.setTotalPages(count/baseQuery.getPageCount());
        }else{
            pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
        }

        //查询集合
        List<Business> businessList = businessDao.getByPage(baseQuery);
        pageBean.setData(businessList);

        return ResultBean.SUCCESS(pageBean);
    }

    @Override
    public ResultBean batchDel(List<Integer> ids) throws Exception {

        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("params null");
        }

        for (Integer id:ids){
            businessDao.delete(id);
        }

        return ResultBean.SUCCESS();
    }
}
